//
//  AlbumListVC.m
//  HuiYinMaster
//
//  Created by new-1020 on 2017/4/13.
//  Copyright © 2017年 Rowling. All rights reserved.
//

#import "AlbumListVC.h"
#import "AlbumListTableViewCell.h"
#import "AlbumDetailViewController.h"
#import "SearchAlbumViewController.h"
#import "UIImageView+WebCache.h"
#import "SDWebImageManager.h"
#import "SeverManager.h"

@interface AlbumListVC ()<UITableViewDelegate,UITableViewDataSource>
{
    int i ;
    int size;
}
//@property (nonatomic,assign) int i;
//@property (nonatomic,strong) NSMutableArray <AlbumListModel *>*dataArray;

@end

@implementation AlbumListVC


-(NSMutableArray<AlbumListModel *> *)albumListDataArray {
    if (_albumListDataArray == nil) {
        _albumListDataArray = [[NSMutableArray alloc]init];
    }
    return _albumListDataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    size = 5;
    i = 0;
    [self creatUI];
    [self loadData];
    [self addFooterRefresh];
}

-(void)XMRefreshLoadMore {
    i = (int)self.albumListDataArray.count;
    [self loadData];
}

-(void)creatUI {
    self.BaseTableView.delegate = self;
    self.BaseTableView.dataSource = self;
    self.BaseTableView.showsVerticalScrollIndicator = false;
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    self.title = @"全部专辑";
    
    UIButton * serarchBtn = [[UIButton alloc]init];
    [serarchBtn setImage:[UIImage imageNamed:@"icon_search"] forState:UIControlStateNormal];
    [serarchBtn sizeToFit];
    [serarchBtn addTarget: self action:@selector(serachBtnClick) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:serarchBtn];
    [self addCenterButton];
}

-(void)loadData {
    NSDictionary * param = @{
                             @"i" : @(i),
                             @"size" : @(size)
                             };
    
    [WYNetTool GET_Urlstring:albumList parameters:param success:^(id responseObject) {
        NSArray * array  = responseObject[@"data"];
        if (array.count > 0) {
            for (NSDictionary * dic in array) {
                AlbumListModel * model = [[AlbumListModel alloc]init];
                [model setValuesForKeysWithDictionary:dic];
                [self.albumListDataArray addObject:model];
            }
            [self.BaseTableView reloadData];
        } else {
            self.noMoredata = YES;
        }
        
        self.isRefresh = NO;
    } fail:^(id error) {
        self.isRefresh = NO;
    }];
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.albumListDataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 160;
}
//-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
//    return 160;
//}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AlbumListTableViewCell * cell =  [AlbumListTableViewCell CellWith:tableView];
    AlbumListModel * model = self.albumListDataArray[indexPath.row];
    [cell.AlbumCellImgaView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",port,model.imgUrl]]];
    cell.AlbumCellTitle.text = model.title;
    cell.AlbumCreateTime.text = [model.createTime substringToIndex:10];
    cell.AlbumPlayTimes.text = [NSString stringWithFormat:@"%d 万",model.playTimes];
    cell.AlbumTotalVoice.text = [NSString stringWithFormat:@"%d 集",model.voiceTotal];
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    AlbumDetailViewController * vc = [[AlbumDetailViewController alloc]init];
    vc.AlbumDtailModel = self.albumListDataArray[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma -mark : click
-(void)serachBtnClick {
    SearchAlbumViewController * searchAlbumVC = [[SearchAlbumViewController alloc]init];
    [self.navigationController pushViewController:searchAlbumVC animated:YES];
}




@end
